<?php

class Disposisi_pta extends Controller {

    function Disposisi_pta() {
        parent::Controller();
        $this->load->model('Disposisi_pta_model', 'p');
    }
    
    function index() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }

        $tabel_header = $this->p->list_header();
        $nama_variabel = 'data';
        $isi = 'var '.$nama_variabel.'='.$this->p->list_isi();
        
        $data['additional_js'] = '
        <script type="text/javascript">
        function deleteRow(id) {
            var txt = \'Anda yakin mau menghapus data disposisi_pta ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';
                url = "'.base_url().'index_arsip.php/disposisi_pta/hapus/"+id;
             $.ajax({
               type: "POST",
               url: "'.base_url().'index_arsip.php/disposisi_pta/bisa_dihapus/"+id,
               success: function(msg) {
                 if (msg==\'bisa\') {
                    $.prompt(txt,{
                        buttons:{Hapus:true, Batal:false},
                        callback: function(v){
                            if(v) {
                                $("tr#row-"+id).fadeOut("slow", function() {
                                    $("tr#row-"+id).remove();
                                });
                                top.location.href=url;
                            }
                        }
                    });
                 } else {
                    $.prompt(\'Maaf, data disposisi_pta tidak dapat dihapus dikarenakan terkait dengan data lain.\');
                 }
               }
            });
        }
        </script>
        <script type="text/javascript">
            '.$isi.'

            // This file demonstrates the different options of the pagination plugin
            // It also demonstrates how to use a JavaScript data structure to
            // generate the paginated content and how to display more than one
            // item per page with items_per_page.

            /**
             * Callback function that displays the content.
             *
             * Gets called every time the user clicks on a pagination link.
             *
             * @param {int}page_index New Page index
             * @param {jQuery} jq the container with the pagination links as a jQuery object
             */
            function pageselectCallback(page_index, jq){
                // Get number of elements per pagionation page from form
                var items_per_page = '.ROW_PER_PAGE.';
                var max_elem = Math.min((page_index+1) * items_per_page, '.$nama_variabel.'.length);
                var newcontent = \''.$tabel_header.'\';

                // Iterate through a selection of the content and build an HTML string
                for(var i=page_index*items_per_page;i<max_elem;i++)
                {
                    newcontent += '.$nama_variabel.'[i];
                }

                newcontent += \'</tbody></table>\';

                // Replace old content with new content
                $("#isi_tabel").html(newcontent);

                //$("#scrollable").tablesorter({sortList: [[1,0]], headers: { 0:{sorter: false}, 2:{sorter: false}, 8:{sorter: false}, 11:{sorter: false} }, widthFixed: true, widgets: [\'zebra\']});

                // Prevent click eventpropagation
                return false;
            }

            // When document has loaded, initialize pagination and form
            $(document).ready(function(){
                $("#paginasi").pagination('.$nama_variabel.'.length, {
                    items_per_page:'.ROW_PER_PAGE.',
                    num_edge_entries: '.NUM_EDGE_ENTRIES.',
                    next_text: \''.TEKS_NEXT.'\',
                    prev_text: \''.TEKS_PREV.'\',
                    next_show_always: '.ALWAYS_SHOW_NEXT.',
                    prev_show_always: '.ALWAYS_SHOW_PREV.',
                    callback:pageselectCallback
                });
            });

        </script>
        ';
        
        $data['judul'] = 'Disposisi';
        $data['tampilkan_menu'] = true;
        $data['link_tambah'] = base_url().'index_arsip.php/disposisi_pta/form_isian/';
        $data['teks_link_tambah'] = 'Lembar Disposisi Baru';
        
        $this->load->view('view_main',$data);
    }

    function _setRules($is_baru=true) {
        return '
                    //set the rules for the field names
                    rules: {
                        kode_surat: { required: true },
                        nomor_surat: { required: true },
                        lokasi_arsip: { required: true },
                        nama_pembuat: { required: true },'.
                       ($is_baru?'namafile: { required: true },':'').
                       'tgl_dokumen: { required: true },
                        tgl_diterima: { required: true }
                    },
                    //set messages to appear inline
                    messages: {
                        kode_surat: "Kode Surat wajib diisi",
                        nomor_surat: "Nomor Surat wajib diisi",
                        lokasi_arsip: "Lokasi Arsip wajib diisi",
                        nama_pembuat: "Nama Pengirim wajib diisi",'.
                        ($is_baru?'namafile: "Nama File wajib diisi",':'').
                       'tgl_dokumen: "Tanggal Dokumen wajib diisi",
                        tgl_diterima: "Tanggal Diterima wajib diisi"
                    }
        ';
    }
    
    function _setJsForm($additional_string,$is_baru=true) {
        $ret = '
        <script type="text/javascript">
            $(function() {
                $("#kode_surat").focus();
//                $(".date-pick").datePicker({startDate:\''.date('d-m-Y').'\'});
                $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'index_arsip.php/disposisi_pta\';
                });
                // validate contact form on keyup and submit
                $("form").validate({
                    '.$this->_setRules($is_baru).'
                });
            });
        </script>
        ';
        return $ret;
    }
    
    function _setCSS() {
        return '
            <style type="text/css">
            a.dp-choose-date {
                float: left;
                width: 16px;
                height: 16px;
                padding: 0;
                margin: 5px 3px 0;
                display: block;
                text-indent: -2000px;
                overflow: hidden;
                background: url('.base_url().'templates/images/cal.gif) no-repeat;
            }
            a.dp-choose-date.dp-disabled {
                background-position: 0 -20px;
                cursor: default;
            }
            /* makes the input field shorter once the date picker code
             * has run (to allow space for the calendar icon
             */
            input.dp-applied {
                width: 140px;
                float: left;
            }
            </style>
        ';
    }
    
    function form_isian() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }
        
        $form_action = base_url().'index_arsip.php/disposisi_pta/simpan';
        $form_id = 'form_disposisi_pta';
        
        $data['additional_js'] = $this->_setJsForm("");
        $data['additional_css'] = $this->_setCSS();
        
        $data['judul'] = 'Disposisi Baru';
        $data['list_data'] = $this->p->entry_form($form_id,$form_action);
        $this->load->view('view_farsip', $data);
    }
    
    function simpan($is_update=FALSE, $id='') {
    /*    if (!$this->input->post('judul')) {
            redirect('disposisi_pta', 'refresh');
        } else {
            $arr['judul'] = $this->input->post('judul');
        }
          */
        $arr['pta_id_kategori_arsip'] = DISPOSISI;
        $arr['deskripsi'] = $this->input->post('deskripsi')?$this->input->post('deskripsi'):"";
        $arr['tgl_dokumen'] = $this->input->post('tgl_dokumen')?DMY2YMD(str_replace("/", TGL_SEPARATOR, $this->input->post('tgl_dokumen')), TGL_SEPARATOR):date('Y-m-d');
        
        $arr_2['kode_surat'] = $this->input->post('kode_surat')?$this->input->post('kode_surat'):"";
        $arr_2['lokasi_arsip'] = $this->input->post('lokasi_arsip')?$this->input->post('lokasi_arsip'):"";
        $arr_2['nomor_arsip'] = $this->input->post('nomor_arsip')?$this->input->post('nomor_arsip'):"";
        $arr_2['nama_pembuat'] = $this->input->post('nama_pembuat')?$this->input->post('nama_pembuat'):"";
        $arr_2['jabatan_pembuat'] = "";//$this->input->post('jabatan_pembuat')?$this->input->post('jabatan_pembuat'):"";
        $arr_2['nama_penerima'] = "";//$this->input->post('nama_penerima')?$this->input->post('nama_penerima'):"";
        $arr_2['jabatan_penerima'] = "";//$this->input->post('jabatan_penerima')?$this->input->post('jabatan_penerima'):"";
        $arr_2['nama_terusan'] = "";//$this->input->post('nama_terusan')?$this->input->post('nama_terusan'):"";
        $arr_2['jabatan_terusan'] = "";//$this->input->post('jabatan_terusan')?$this->input->post('jabatan_terusan'):"";
        $arr_2['tembusan'] = "";//$this->input->post('tembusan')?$this->input->post('tembusan'):"";
        $arr_2['tgl_diterima'] = $this->input->post('tgl_diterima')?DMY2YMD(str_replace("/", TGL_SEPARATOR, $this->input->post('tgl_diterima')), TGL_SEPARATOR):date('Y-m-d');
        $arr_2['tgl_arsip'] = $arr['tgl_dokumen'];

        if ($is_update) {
            if ($_FILES['namafile']['name'] != "") {
                /* sebelum update file, rename dulu file lama
                 * format file lama: <id_file><YYYYmmddHHmmss><nama_file>
                 */
                $this->load->model('Datum_model', 'd');
        
                $id_file = $this->d->getData('pta_id_files', 'arsip', array('idpta_arsip'=>$id));
                $nama_file = $this->d->getData('namafile', 'files', array('idpta_files' => $id_file));
                if (file_exists(BASE_UPLOAD_ARSIP.$id_file.$nama_file)) {
                    rename( BASE_UPLOAD_ARSIP.$id_file.$nama_file, BASE_UPLOAD_ARSIP.$id_file.date('YmdHis').$nama_file);
                }
            }
        }
        
        if ($_FILES['namafile']['name'] != "") {
            $config['upload_path'] = BASE_UPLOAD_ARSIP;
            $config['allowed_types'] = UPLOAD_ALLOWED_FILETYPE;
            $config['max_size']    = UPLOAD_SIZE_MAKSIMUM;
            
            $this->load->library('upload', $config);
            if ( $this->upload->do_upload('namafile') ) {
                $data_uplot = $this->upload->data();
                
                $arr_file['namafile'] = $data_uplot['file_name'];
                $arr_file['ukuran'] = $data_uplot['file_size'];
                $arr_file['ekstensi'] = $data_uplot['file_ext'];
                $arr_file['tipe_file'] = $data_uplot['file_type'];
                $arr_file['pta_user_namauser'] = $this->session->userdata('username'); 
                if ($is_update) {
                    $this->db->where(array('idpta_files' => $id_file));
                    $this->db->update('files', $arr_file);
                } else {
                    $this->db->insert('files', $arr_file);
                    $arr['pta_id_files'] = $this->db->insert_id();
                    $id_file = $arr['pta_id_files'];
                }

                /* rename file di server
                 * format file : <id_file><nama_file>
                 */
                rename( BASE_UPLOAD_ARSIP.$arr_file['namafile'], BASE_UPLOAD_ARSIP.$id_file.$arr_file['namafile']);
            } else {
                die($this->upload->display_errors());
            }
        }

        $arr['user_update'] = $this->session->userdata('username'); 
        if ($is_update) {
            $this->p->simpan($arr, $arr_2, $is_update, $id);
        } else {
            $arr['user_input'] = $this->session->userdata('username'); 
            $arr['tgl_input'] = date('Y-m-d H:i:s'); 
            $this->p->simpan($arr, $arr_2);
        }

        redirect('disposisi_pta');
    }
    
    function form_edit($id) {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }
        
        $data['additional_js'] = $this->_setJsForm("",false);
        $data['additional_css'] = $this->_setCSS();
        
        $this->db->where(array('idpta_arsip' => $id));
        $q = $this->db->get('arsip');
        if ($q->num_rows() > 0) {
            $row = $q->row();
            $arr['judul']                  = $row->judul;
            $arr['deskripsi']              = $row->deskripsi;
            $arr['tgl_dokumen']            = YMD2DMY(trimTime($row->tgl_dokumen), TGL_SEPARATOR);
            
            $this->load->model('Datum_model', 'd');
            $arr['namafile']               = $this->d->getData('namafile', 'files', array('idpta_files' => $row->pta_id_files));
            
            $arr['created_by']             = sprintf('%s pada %s', $row->user_input, YMD2Indo(trimTime($row->tgl_input), TGL_SEPARATOR));
            if ($row->user_update!='') {
                $arr['last_update']        = sprintf('%s pada %s', $row->user_update, YMD2Indo(trimTime($row->tgl_update), TGL_SEPARATOR));
            }
            
            $this->db->where(array('pta_id_arsip' => $row->idpta_arsip));
            $q0 = $this->db->get('arsip_surat');
            $row0 = $q0->row();
            $arr['kode_surat'] = $row0->kode_surat;
            $arr['nomor_arsip'] = $row0->nomor_arsip;
            $arr['lokasi_arsip'] = $row0->nomor_arsip;
            $arr['nama_pembuat'] = $row0->nama_pembuat;
            $arr['jabatan_pembuat'] = $row0->jabatan_pembuat;
            $arr['nama_penerima'] = $row0->nama_penerima;
            $arr['jabatan_penerima'] = $row0->jabatan_penerima;
            $arr['nama_terusan'] = $row0->nama_penerima;
            $arr['jabatan_terusan'] = $row0->jabatan_penerima;
            $arr['tgl_arsip'] = YMD2DMY(trimTime($row0->tgl_arsip), TGL_SEPARATOR);
            $arr['tembusan'] = $row0->tembusan;
            
            $form_action = base_url().'index_arsip.php/disposisi_pta/simpan/true/'.$id;
            $form_id = 'form_disposisi_pta';

            $data['judul'] = 'Ubah Disposisi';
            $data['list_data'] = $this->p->entry_form($form_id,$form_action,$arr);
            $this->load->view('view_farsip', $data);
        } else {
            redirect('disposisi_pta', 'refresh');
        }
    }


    function hapus($id) {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }
        
        $this->load->model('Datum_model', 'd');
        $id_file = $this->d->getData('pta_id_files', 'arsip', array('idpta_arsip'=>$id));
        
        $this->db->where('idpta_arsip', $id);
        $this->db->delete('arsip');
        if ($this->db->affected_rows()>0) {
            $namafile = $this->d->getData('namafile', 'files', array('idpta_files'=>$id_file));
            rename( BASE_UPLOAD_ARSIP.$id_file.$namafile, BASE_UPLOAD_ARSIP.'_del'.date('YmdHis').$id_file.$namafile );
            
            $this->db->where('idpta_files', $id_file);
            $this->db->delete('files');
        }
        redirect('disposisi_pta', 'refresh');
    }
    
    
    function preview($id) {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }
        
        $this->load->model('Datum_model', 'd');
        $id_file = $this->d->getData('pta_id_files', 'arsip', array('idpta_arsip'=>$id));
        $namafile = $this->d->getData('namafile', 'files', array('idpta_files'=>$id_file));
        
        $data['judul'] = $this->d->getData('judul', 'arsip', array('idpta_arsip'=>$id));
        $data['isi'] = '<center><p><img src="'.base_url().'uploads/arsip/'.$id_file.$namafile.'" /><p></center>';
        $data['link_kembali'] = 'javascript:window.close();';
        $this->load->view('view_blank', $data);
    }
    
    function bisa_dihapus($id) {
        echo 'bisa';
    }
    
    
    function buat_disposisi($id) {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }
        
        $data['additional_js'] = $this->_setJsForm("",false);
        $data['additional_css'] = $this->_setCSS();
        
        $this->db->where(array('idpta_arsip' => $id));
        $q = $this->db->get('arsip');
        if ($q->num_rows() > 0) {
            $row = $q->row();
            $arr['judul']                  = $row->judul;
            $arr['deskripsi']              = $row->deskripsi;
            $arr['tgl_dokumen']            = YMD2DMY(trimTime($row->tgl_dokumen), TGL_SEPARATOR);
            
            $this->load->model('Datum_model', 'd');
            $arr['namafile']               = $this->d->getData('namafile', 'files', array('idpta_files' => $row->pta_id_files));
            $arr['id']               = $row->pta_id_files;
            $arr['created_by']             = sprintf('%s pada %s', $row->user_input, YMD2Indo(trimTime($row->tgl_input), TGL_SEPARATOR));
            if ($row->user_update!='') {
                $arr['last_update']        = sprintf('%s pada %s', $row->user_update, YMD2Indo(trimTime($row->tgl_update), TGL_SEPARATOR));
            }
            $id_arsip_surat = $row->idpta_arsip; 
            $arr['id_arsip_surat'] = $row->idpta_arsip;
            
            $this->db->where(array('pta_id_arsip' => $row->idpta_arsip));
            $q0 = $this->db->get('arsip_surat');
            $row0 = $q0->row();
            $arr['kode_surat'] = $row0->kode_surat;
            $arr['nomor_arsip'] = $row0->nomor_arsip;
            $arr['lokasi_arsip'] = $row0->nomor_arsip;
            $arr['nama_pembuat'] = $row0->nama_pembuat;
            $arr['jabatan_pembuat'] = $row0->jabatan_pembuat;
            $arr['nama_penerima'] = $row0->nama_penerima;
            $arr['jabatan_penerima'] = $row0->jabatan_penerima;
            $arr['nama_terusan'] = $row0->nama_penerima;
            $arr['jabatan_terusan'] = $row0->jabatan_penerima;
            $arr['tgl_arsip'] = YMD2DMY(trimTime($row0->tgl_arsip), TGL_SEPARATOR);
            $arr['tembusan'] = $row0->tembusan;
            /*
           // die($id_arsip_surat);         
            $this->db->where(array('id_arsip_surat' => $id_arsip_surat));
            $q1 = $this->db->get('arsip_terusan');
            $x=1;
            foreach($q1->result() as $row1){     
            $arr['diteruskan_kepada'] = $x.'. '.$row1->diteruskan_kepada; 
            $x++;
            }
            */
            $form_action = base_url().'index_arsip.php/disposisi_pta/simpan_baru/true/'.$id;
            $form_id = 'form_disposisi_pta';

            $data['judul'] = 'Disposisi Surat';
            $data['list_data'] = $this->p->entry_disposisi($form_id,$form_action,$arr);
            $this->load->view('view_farsip', $data);
        } else {
            redirect('disposisi_pta', 'refresh');
        }
    }
    
    function simpan_baru($is_update=FALSE, $id='') {

        $arr_1['diteruskan_kepada'] = $this->input->post('nama_terusan')?$this->input->post('nama_terusan'):"";
        $arr_1['id_arsip_surat'] = $id;
        $arr_1['username'] = $this->session->userdata('username');       
        $arr_1['isi_disposisi'] = $this->input->post('isi_disposisi')?$this->input->post('isi_disposisi'):"";
        //$arr_2['username'] = $this->session->userdata('username');
        
       // if ($arr_1['diteruskan_kepada'] != ""){        
        $this->db->insert('arsip_terusan', $arr_1);
        //}
        
      /*  if ($arr_2['isi_disposisi'] != "" && $arr_1['diteruskan_kepada'] != "") {
        $arr_2['id_terusan'] = $this->db->insert_id();
        $this->db->insert('arsip_disposisi', $arr_2);
        }
                
        if ($is_update) {
            $this->p->simpan_disposisi($arr, $arr_1, $arr_2, $is_update, $id);
        } else {
            $arr['user_input'] = $this->session->userdata('username'); 
            $arr['tgl_input'] = date('Y-m-d H:i:s'); 
            $this->p->simpan_disposisi($arr, $arr_1, $arr_2);
        }
      */
        redirect('disposisi_pta');
    }
}

/* End of file disposisi_pta.php */
/* Location: ./system/application/arsip/controllers/disposisi_pta.php */